<template>
	<view>
		
		<uni-group v-if=" '0'== type " title="修改密码:" top="0">
			<uni-forms-item>
				<uni-easyinput type="text" v-model="formData.password" placeholder="请输入新的密码"></uni-easyinput>
			</uni-forms-item>
		</uni-group>
		
		<uni-group v-if=" '1'== type " title="修改姓名:" top="1">
			<uni-forms-item>
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入新的姓名"></uni-easyinput>
			</uni-forms-item>
		</uni-group>
		
		<uni-group v-if=" '2'== type " title="修改手机:" top="1">
			<uni-forms-item>
				<uni-easyinput type="text" v-model="formData.number" placeholder="请输入新的手机号"></uni-easyinput>
			</uni-forms-item>
		</uni-group>
		
		<view class="uni-padding-wrap uni-common-mt">
		      <button type="primary" class="button-sp-area" @click="submit" >提交</button>     
		</view>
		
		
		

	</view>
</template>

<script>
	export default {
		data() {
			return {
				name_new : '',
				number_new: '',
				password_new: '',
				type: '',
				formData: {
					name: '',
					number: '',
					password: ''
				}
			}
		},
		onLoad: function (option) {
		    this.type = option.type
		},
		methods: {
			submit(){
				var _this = this
				let user = uni.getStorageSync('userMess');
				let object = uni.getStorageSync('loginObjet');
				
				//登录
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.formData.name.length == "") {
				   this.name_new = user.name
				} else {
					this.name_new = this.formData.name
				}
				
				if (this.formData.number.length == "") {
				    this.number_new = user.tel
				} else {
					this.number_new = this.formData.number
				}
				
				if (this.formData.password.length < 6 && this.formData.password == '' ) {
					this.password_new = object.password
				} else {
					var b = new Buffer(this.formData.password)
					this.password_new = b.toString('base64')
				}
				
				_this.isRotate=true
				
				
				uni.request({
				    url: this.$httpUrl +'/admin/Admin/modifyUser', //仅为示例，并非真实接口地址。
				    method: 'POST',
					header:{
						token: user.token
					},
					data: {
						Id: user.Id,
						password: this.password_new,
						name: this.name_new,
						tel: this.number_new,
						mail: user.mail
				    },
				    success: (res) => { //请求正常
				        console.log(res.data);
						if(res.data.code == 0){
							this.$uniApi.open('修改成功！')
							this.clear()
							//修改成功后，跳转login重新登陆
							this.$to.jumpTo('/login/login')
						} else {
							this.$uniApi.open(res.data.msg)
						}
				     
				    },
					fail: (res) => { //异常
						this.$uniApi.open(res.data.msg)
					},
					complete: (res) =>{ //最终执行
						this.clear()
					}
				});
			},
			clear(){ //清楚状态
				this.formData.password = ''
				this.formData.name = ''
				this.formData.number = ''
				
				uni.removeStorageSync('userMess')
				uni.removeStorageSync('loginObjet')
				uni.removeStorageSync('token')
			}
		}
	}
</script>

<style>
	.button-sp-area{
		margin: 0 auto;
		width: 60%;
		background-color:#014b8c
	}

</style>
